import type {
  DoctorPage,
  FollowType,
  Image,
  KnowledgePage,
  KnowledgeParams,
  PageParams,
  TopDep,
  ConsultOrderPreData,
  ConsultOrderPreParams,
  PartialConsult,
  ConsultOrderItem,
} from '@/types/consult'
import { request } from '@/utils/request'

import type { ConsultOrderListParams, ConsultOrderPage } from '@/types/consult'

// 删除订单
export const deleteOrder = (id: string) =>
  request({
    method: 'DELETE',
    url: `/patient/order/${id}`,
  })

// 取消订单
export const cancelOrder = (id: string) =>
  request({
    method: 'PUT',
    url: `/patient/order/cancel/${id}`,
  })

export const getConsultOrderList = (params: ConsultOrderListParams) =>
  request<ConsultOrderPage>({
    url: '/patient/consult/order/list',
    data: params,
  })

// 评价问诊
export const evaluateConsultOrder = (data: {
  docId: string
  orderId: string
  score: number
  content: string
  anonymousFlag: 0 | 1
}) =>
  request<{ id: string }>({
    method: 'POST',
    url: '/patient/order/evaluate',
    data,
  })

// 查看处方
export const getPrescriptionPic = (id: string) =>
  request<{ url: string }>({ url: `/patient/consult/prescription/${id}` })

// 获取问诊订单详情
export const getConsultOrderDetail = (orderId: string) =>
  request<ConsultOrderItem>({
    url: '/patient/consult/order/detail',
    data: { orderId },
  })

/**
 * 获取支付地址
 * @param params.paymentMethod  0 是微信  1 支付宝
 * @returns
 */
export const getConsultOrderPayUrl = (params: {
  paymentMethod: 0 | 1
  orderId: string
  payCallback: string
}) =>
  request<{ payUrl: string }>({
    method: 'POST',
    url: '/patient/consult/pay',
    data: params,
  })

// 生成订单
export const createConsultOrder = (data: PartialConsult) =>
  request<{ id: string }>({
    method: 'POST',
    url: '/patient/consult/order',
    data,
  })

// 拉取预支付订单信息
export const getConsultOrderPre = (params: ConsultOrderPreParams) =>
  request<ConsultOrderPreData>({
    url: '/patient/consult/order/pre',
    data: params,
  })

export const uploadImage = (file: File) => {
  const fd = new FormData()
  fd.append('file', file)
  return request<Image>({
    method: 'POST',
    url: '/upload',
    data: fd,
  })
}

export const getAllDep = () => request<TopDep[]>({ url: '/dep/all' })

export const followOrUnfollow = (id: string, type: FollowType = 'doc') =>
  request({
    method: 'POST',
    url: '/like',
    data: {
      id,
      type,
    },
  })

export const getDoctorPage = (params: PageParams) =>
  request<DoctorPage>({ url: '/home/page/doc', data: params })

export const getKnowledgePage = (params: KnowledgeParams) =>
  request<KnowledgePage>({
    url: '/patient/home/knowledge',
    data: params,
  })
